package com.hdu.dwh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hdu.dwh.dtos.SubjectDatabaseDto;
import com.hdu.dwh.pojos.*;
import com.hdu.dwh.mapper.SubjectDatabaseMapper;
import com.hdu.dwh.service.IDatabaseAllService;
import com.hdu.dwh.service.ISubjectDatabaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdu.dwh.service.ISubjectDatabaseSourceService;
import com.hdu.dwh.service.ISubjectTableService;
import com.hdu.dwh.vos.*;
import com.hdu.dwh.constants.CommonConstants;
import com.hdu.dwh.constants.ParamConstants;
import com.hdu.dwh.enums.HttpStatusEnum;
import com.hdu.dwh.exception.CustomException;
import com.hdu.dwh.results.ResponseResult;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 主题数据库表 服务实现类
 * </p>
 *
 * @author bobo
 * @since 2025-04-08
 */
@Service
@RequiredArgsConstructor
public class SubjectDatabaseServiceImpl extends ServiceImpl<SubjectDatabaseMapper, SubjectDatabase> implements ISubjectDatabaseService {

    private final ISubjectDatabaseSourceService subjectDatabaseSourceService;
    private final IDatabaseAllService databaseAllService;
    @Lazy
    @Autowired
    private ISubjectTableService subjectTableService;

    /**
     * 新增主题数据库
     * @param subjectDatabaseDto
     * @return
     */
    @Override
    @Transactional
    public ResponseResult add(SubjectDatabaseDto subjectDatabaseDto) {
        if (subjectDatabaseDto == null) {
            return ResponseResult.fail(CommonConstants.PARAM_IS_NULL);
        }
        // 检查数据库是否已存在
        if(databaseAllService.existsDatabase(subjectDatabaseDto.getDatabaseName())){
            return ResponseResult.fail(CommonConstants.DATABASE_ALL_EXIST);
        }
        SubjectDatabase subjectDatabase = new SubjectDatabase();
        BeanUtils.copyProperties(subjectDatabaseDto, subjectDatabase);
        if (!save(subjectDatabase)) {
            return ResponseResult.fail(CommonConstants.ADD_FAILED);
        }
        // 存总数据库表
        DatabaseAll databaseAll = new DatabaseAll()
                .setDatabaseType(ParamConstants.SUBJECT_DATABASE)
                .setDatabaseId(subjectDatabase.getId())
                .setDatabaseName(subjectDatabase.getDatabaseName());
        if (!databaseAllService.save(databaseAll)){
            throw new CustomException(HttpStatusEnum.INSERT_ERROR);
        }
        return ResponseResult.success(CommonConstants.ADD_SUCCESS);
    }

    /**
     * 查询主题数据库详情
     * @param id
     * @return
     */
    @Override
    public ResponseResult getDetilById(Long id) {
        if (id == null) {
            return ResponseResult.fail(CommonConstants.PARAM_IS_NULL);
        }
        SubjectDatabase getById = getById(id);
        if (getById == null) {
            return ResponseResult.fail(CommonConstants.RECORD_NOT_EXIST);
        }
        // 源库信息查询拼接
        SubjectDatabaseVo subjectDatabaseVo = new SubjectDatabaseVo();
        BeanUtils.copyProperties(getById, subjectDatabaseVo);
        LambdaQueryWrapper<SubjectDatabaseSource> wrapper = new LambdaQueryWrapper<SubjectDatabaseSource>()
                .eq(SubjectDatabaseSource::getSubjectDatabaseId, id);
        List<SubjectDatabaseSource> subjectDatabaseSources = subjectDatabaseSourceService.list(wrapper);
        List<SubjectDatabaseSourceVo> sourceDatabases = subjectDatabaseSources.stream().map(source -> {
            SubjectDatabaseSourceVo vo = new SubjectDatabaseSourceVo();
            BeanUtils.copyProperties(source, vo);
            return vo;
        }).toList();
        subjectDatabaseVo.setSourceDatabases(sourceDatabases);
        return ResponseResult.success(subjectDatabaseVo);
    }

    /**
     * 删除主题数据库
     * @param id
     * @return
     */
    @Override
    @Transactional
    public ResponseResult deleteById(Long id) {
        if (id == null) {
            return ResponseResult.fail(CommonConstants.PARAM_IS_NULL);
        }
        // 删除主题数据库
        if (!removeById(id)) {
            return ResponseResult.fail(CommonConstants.DELETE_FAILED);
        }
        // 删总记录库
        if (!databaseAllService.delete(ParamConstants.SUBJECT_DATABASE, id)){
            throw new CustomException(HttpStatusEnum.DELETE_FAILED);
        }
        // 删溯源库
        if (!subjectDatabaseSourceService.deleteByDatabaseId(id)){
            throw new CustomException(HttpStatusEnum.DELETE_FAILED);
        }
        // 删表
        if (!subjectTableService.deleteByDatabaseId(id)){
            throw new CustomException(HttpStatusEnum.DELETE_FAILED);
        }
        return ResponseResult.success(CommonConstants.DELETE_SUCCESS);
    }
}
